<?php
/**
 * @author Mingming Tang iamalazyrat@gmail.com
 */

class Category_Controller extends Base_Controller {

  public $restful = true;

  public function get_index() {
    return Redirect::to(URL::to('category/list'));
  }

  public function get_list() {
    $c = new TopClient;
    $c->appkey = Config::get('option.appkey');
    $c->secretKey = Config::get('option.secret');
    $req = new ItemcatsGetRequest;
    $req->setFields("cid,parent_cid,name,is_parent, status");
    $req->setParentCid(0);
    $resp = $c->execute($req);
    $categories = $resp->item_cats;
    return View::make('category.list.main')->with('categories', $categories);
  }

  public function get_list_enable() {
    $categories = Category::all();
    return View::make('category.list_enable.main')->with('categories', $categories);
  }

  public function post_op() {
    $op = Input::get('op', false); 
    $data = Input::get('data', false);
    if (!$op) return Response::json(array('code' => 400));
    if (!$data OR !isset($data['cid']) OR !isset($data['name'])) return Response::json(array('code' => 400));
    switch ($op) {
      case 'enable':
	$category = new Category();
        $category->name = $data['name'];
        $category->cid = $data['cid'];
        $category->status = $data['status'];
        $category->save();
	return Response::json(array('code'=>200));
        break;
      case 'disable':
        $c = Category::where('cid', '=', $data['cid'])->first();
        $c->delete();
	return Response::json(array('code'=>200));
        break;
      default:
        return Response::json(array('code' => 400));
        break;
    }
    // never go here
    return Response::json(array('code' => 400));
  }

}
